"""Catchall methods to attempt a list or edit on any class added to the model"""

from django.template import loader, RequestContext
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.shortcuts import get_object_or_404
from django.utils.translation import ugettext_lazy as _
from django.db.models.loading import get_model
from django.conf import settings
from duetopia.register.views.generic import MDSearchList
from urllib import urlencode

__all__ = ('description','opensearch_list')

def description(request,template='register/opensearch/description.xml'):
    context = {
        'baseurl' : settings.WWW_BASEURL,
    }
    c = RequestContext(request, context)  
    t = loader.get_template(template)
    return HttpResponse(content=t.render(c),content_type='text/xml')

def opensearch_list(request,template='register/opensearch/results_atom.xml'):
    """Search datasets/sources. (See MDSearchList)
    Returns an Opensearch / Atom list of result sets"""
    model = get_model('register','dataset')
    params = dict(request.GET.items())
    query = urlencode(params)
    previous = next = last = None
    # use our custom django-admin ChangeList to search - see generic.py
    cl = MDSearchList(request, model)
    last = cl.paginator.pages - 1

    if cl.paginator.has_previous_page(cl.page_num) is True:
        previous = cl.page_num - 1
    if cl.paginator.has_next_page(cl.page_num) is True:
        next = cl.page_num + 1
    
    feed_uuid = updated = None # should be per-query? or per result-set?
    # see onlineresource.py SearchResults - option of caching query results
    # / tradeoff with generating new UUID + update date for Atom "conformance"
    context = {
        'model' : model._meta,
        'cl' : cl, 
        'query': query,
        'params': params,
        'startIndex': cl.paginator.first_on_page(cl.page_num),
        'previous' : previous,
        'next' : next,
        'last': last,
        'baseurl' : settings.WWW_HOST+settings.WWW_BASEURL,
        'uuid' : feed_uuid,
        'updated': updated,
    }

    """The Atom feed specification insists that we MUST have a uuid and updated date
    for feeds. More appropriate for aggregation of a stream of resources (RSS model)
    than for use as part of a carrier format for a one-off query response.
    We can generate an arbitrary uuid and updated date (eg now) for each response
    but what would be the point? (compliance with the letter, but not the spirit)
    
    We could keep a cache of queries. Sometimes we'd see the parameters in a different 
    order, and get a different cache, but this wouldn't really matter.
    How would we tell whether the result set had changed? 
    We'd *assume* that results wouldn't disappear from the db and so any 
    change in a result set would have to be accompanied by an increment in
    the result count."""
    
    c = RequestContext(request, context)
    t = loader.get_template(template)
    return HttpResponse(content=t.render(c),content_type='text/xml')


